<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\common\controller\Backend;

class Slide extends Backend
{
    public function initialize() {
        parent::initialize();
        $this->_mod  = new \app\admin\model\Slide();
    }
    //上传图片
    public function ajax_upload_img(){
        $file = request()->file('file');  //这里‘file’是你提交时的name
        $info = [
            'ext'=>$file->extension(),
            'filesize' => $file->getSize(),    // 文件名：读书顶个鸟用.jpg
            'name' => $file->getFilename(),    // 文件MINE：image/jpeg
            'mime' => $file->getMime(),
            'oldname'=>$file->getOriginalName()
        ];

        try{
            $saveName = \think\facade\Filesystem::disk('public')->putFile( 'slide', $file,
                function () use ($file){
                    switch ('date') {
                        case 'date':
                            $fileName = date('YmdHis').rand(1,99);
                            break;
                        // 此项即为：保留原始文件名+日期方式
                        case 'original':
                            $fileName = str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName().date('YmdHis'));
                            break;
                        default:
                            $fileName = uniqid();
                            break;
                    }
                    return $fileName;
                });//保存文件名
            $newname= str_replace('slide/','',$saveName);
            $arr = ['status' => 1, 'msg' => '成功', 'info' => $newname,'img_url'=>upload_url($newname,  'slide')];
            return json($arr);   				//返回标准json格式
        }catch (\Exception $e) {
            return $this->exceptionHandle($e,'上传失败!' . $e->getMessage(),'json','');
        }
    }



}


